#include "slicingsettingsdialog.h"
#include "ui_slicingsettingsdialog.h"

SlicingSettingsDialog::SlicingSettingsDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SlicingSettingsDialog)
{
    ui->setupUi(this);
    iniUI();
}

SlicingSettingsDialog::~SlicingSettingsDialog()
{
    delete normalVectorValidator;
    delete positiveFloatValidator;
    delete ui;
}

void SlicingSettingsDialog::iniUI()
{
    // 初始化值为默认法向量(0,1,0)
    setPlaneNormal(QVector3D(0, 1, 0));

    // 初始化切片厚度为0.01f
    setSlicingStride(0.01f);

    // 设置输入验证器（允许小数和负数）
    normalVectorValidator = new QRegularExpressionValidator(QRegularExpression(R"(^-?\d*\.?\d+$)"), this);
    positiveFloatValidator = new QRegularExpressionValidator(QRegularExpression(R"(^[+]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)$)"), this);
    ui->normalXEdit->setValidator(normalVectorValidator);
    ui->normalYEdit->setValidator(normalVectorValidator);
    ui->normalZEdit->setValidator(normalVectorValidator);
    ui->slicingStride->setValidator(positiveFloatValidator);

    // 设置相机距离范围
    ui->cameraDistanceSlider->setRange(1, 1000); // 0.1-100.0映射为1-1000
    ui->cameraDistanceSlider->setValue(700);     // 默认值70.0
    ui->cameraDistanceSlider->setPageStep(1);
}

QVector3D SlicingSettingsDialog::planeNormal() const
{
    return QVector3D(
               ui->normalXEdit->text().toFloat(),
               ui->normalYEdit->text().toFloat(),
               ui->normalZEdit->text().toFloat()
               ).normalized(); // 确保返回单位向量
}

QVector3D SlicingSettingsDialog::cameraUp() const
{
    return m_camera_up;
}

float SlicingSettingsDialog::slicingStride() const
{
    return ui->slicingStride->text().toFloat();
}

float SlicingSettingsDialog::cameraDistance() const
{
    return ui->cameraDistanceSlider->value() / 10.0f; // 映射回0.1-100.0范围
}

void SlicingSettingsDialog::setPlaneNormal(const QVector3D &normal)
{
    QVector3D n = normal.normalized();
    ui->normalXEdit->setText(QString::number(n.x(), 'f', 2));
    ui->normalYEdit->setText(QString::number(n.y(), 'f', 2));
    ui->normalZEdit->setText(QString::number(n.z(), 'f', 2));
}

void SlicingSettingsDialog::setCameraUp(const QVector3D &cameraUp)
{
    m_camera_up = cameraUp;
}

void SlicingSettingsDialog::setCameraDistance(float distance)
{
    ui->cameraDistanceSlider->setValue(qRound(distance * 10));
}

void SlicingSettingsDialog::setSlicingStride(float stride)
{
    ui->slicingStride->setText(QString::number(stride, 'f', 2));
}

void SlicingSettingsDialog::on_settingConfirmed_accepted()
{
    // 获取当前设置
    QVector3D normal = planeNormal();
    QVector3D camera_up = cameraUp();
    float distance = cameraDistance();
    float stride = slicingStride();

    // 发射信号传递数据
    emit slicingSettingsConfirmed(normal, camera_up, distance, stride);

    // 关闭对话框
    this->accept();
}

void SlicingSettingsDialog::on_settingConfirmed_rejected()
{
    this->close();
}

void SlicingSettingsDialog::on_cameraDistanceSlider_sliderMoved(int position)
{
    double percentage = position / 10.0f;
    ui->fov_lbl->setText(QString("FOV: %1%").arg(percentage, 0, 'f', 2));
}

